from osgeo import ogr
import os

shpfile = os.path.join(os.getcwd(),"include","dataset","shp.shp")

if os.path.exists(shpfile):
    drv: ogr.Driver = ogr.GetDriverByName("Esri shapefile")
    dataSource:ogr.DataSource = drv.Open(shpfile, 0)
    layer: ogr.Layer = dataSource.GetLayer(0)
    layerDefn:ogr.FeatureDefn = layer.GetLayerDefn()
    field_list = []
    for index in range(layerDefn.GetFieldCount()):
        field_defn:ogr.FieldDefn = layerDefn.GetFieldDefn(index)
        field_name = field_defn.GetName()
        field_type = field_defn.GetTypeName()
        field_width = field_defn.GetWidth()
        field_list.append({'field_name':field_name, 'field_type':field_type, 'field_width': field_width})
    
    for feature in layer:
        print("----------------")
        feature: ogr.Feature
        for field in field_list:
            value = feature.GetFieldAsString(field["field_name"])
            print(field["field_name"] + "----" + value)
else:
    print("数据不存在！")